$().ready(function(){
	$(".delblog").each(function(){
			$(this).bind("click",function(){delBlog(this);});
	});
	
	$(".revert_revert").each(function(){
			$(this).bind("click",function(){return revert(this);});
	});
	
	$('.revertpost').click(function() {
			postRevert(this);
	});
	
	$(".delrevert").each(function(){
			$(this).bind("click",function(){return delRevert(this);});
	});
});



function revert(obj){
	var	 html =	'<div><p><textarea id="revertcontent" name="content" cols="30" rows="6"></textarea>';
	html += '</p><p><input type="hidden" id="blogid" value="'+$("#blogid").val()+'"/>';
	html += '<input type="hidden" value="'+$("#blogid").next().val()+'"/>';
	html += '<input type="button" class="revertpost" value="回复"/>';
	html += '<input type="hidden" value="'+$(obj).attr("id")+'"/>';
	html += ' <input type="button" class="revertcancel" value="取消"/>';
	html += '</p</div>';
	if($(".revertcancel").val() == "取消"){
		$(".revertcancel").parent().parent().remove();
	}
	$(obj).parent().next().after(html);
	$(obj).parent().next().next().find("textarea").focus();
	$(obj).parent().next().next().find("textarea").val("对<a href='#revert"+$(obj).attr("id")+"'>"+$(obj).prev().prev().text()+"</a>的回复<br/>");
	var oFCKeditor = new FCKeditor( "revertcontent","90%","160px","Basic") ;
	oFCKeditor.BasePath = ROOT+"/Public/Js/FCKeditor/" ; 
	oFCKeditor.ReplaceTextarea() ;
	$('.revertpost').click(function() {
			postRevert(this);
	});
	$('.revertcancel').click(function() {
			postCancel(this);
	});
	return false;
	
}
/*function resetEditor(){
	setContents("revertcontent",document.getElementById("revertcontent").value)
}; 
function saveEditor(){
	document.getElementById("revertcontent").value = getContents("revertcontent");
} */
function postCancel(obj){
	$(obj).parent().parent().remove();
}

function postRevert(obj){
	var reid = $(obj).next().val();
	if(reid==0){
		var content = getContents("content");
		//$(obj).parent().prev().children("#content").val(value);
		//var content = $(obj).parent().prev().children("#content").val()
	}else{
		var content= getContents("revertcontent");
		//$(obj).parent().prev().children("#revertcontent").val(value);
		//var content = $(obj).parent().prev().children("#revertcontent").val()
	}
	if($.trim(content) == ""){		
		return false;
	}else{
		$(obj).attr("disabled", "disabled");
		$.post(APP+"/blog/saverevert", {ajax:1,blogid: $("#blogid").val(), userid: $(obj).prev().val(), reid: reid, content: content },
			function(data){
				if("error" == data){
					$("#messageBox").remove();
					msg(obj,"操作失败");
					$(obj).removeAttr("disabled");
				}else{
					show(obj,data,content);
				}
		});
	}
}

function show(obj,data,content){
	if(data == "error"){
		$("#messageBox").remove();
		msg(obj,"操作失败");
	}else{
		var s = data.split(",");
		var html = '<div class="revertlist"><div class="revert_username_face">';
		html += '<img src="'+ROOT+'/Public/Uploads/face/small/'+USERFACE+'"/></div>';
		html += '<div class="revertcontent"><div class="reverttitle">';
		html += '<a href="'+APP+'/home/'+s[1]+'">'+s[2]+'</a> 刚刚 <a href="#" class="delrevert" id="'+s[0]+'">删除</a></div>';
		html += '<div class="revert_content">'+content+'</div>';
		html += '</div></div>';
		if($(".revertcancel").val() == "取消"){
			$(".revertcancel").parent().parent().parent().parent().parent().append(html);
			$(".revertcancel").parent().parent().remove();
			setContents("revertcontent","");
		}else{
			$(obj).parent().parent().parent().prev().append(html);
			$(obj).parent().prev().children("#revertcontent").val("");
			setContents("content","");
		}
		$(".delrevert").bind("click",function(){return delRevert(this);});	
	}
	$(obj).removeAttr("disabled");
}

function delRevert(obj){
	alertMessage(obj,"确定删除");
	$(".msgOk").bind("click",function(){delRevertAction(obj);});
	return false;
}

function delRevertAction(obj){
	$.post(APP+"/blog/delrevert", {ajax:1,revertid: $(obj).attr("id")} , 
	function(data){
		if("success"==data){
			$(obj).parent().parent().parent().fadeOut(2000); 
			$("#messageBox").remove();
		}else{
			$("#messageBox").remove();
			msg(obj,"操作失败");
		}
	});
}